import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

class StartupVideo extends StatefulWidget {
  const StartupVideo({super.key, required this.nextWidget});
  final Widget nextWidget;

  @override
  State<StartupVideo> createState() => _StartupVideoState();
}

class _StartupVideoState extends State<StartupVideo> {
  late VideoPlayerController _controller;
  bool lastPlaying = false;
  // final Widget nextWidget;
  bool requestPermission = false;

  _StartupVideoState();

  @override
  Widget build(BuildContext context) {
    return Container(
      child: _controller.value.isInitialized
          ? VideoPlayer(_controller)
          : Container(),
    );
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  void initState() {
    super.initState();
    _controller = VideoPlayerController.asset('assets/video_1.mp4')
      ..addListener(callback)
      ..initialize().then((_) {
        setState(() {});
        _controller.play();
      });
  }

  void callback() {
    // debugPrint('视频控件播放回调 ${_controller.value}');
    if (_controller.value.isCompleted) {
      _controller.removeListener(callback);

      Navigator.of(context).pushReplacement(PageRouteBuilder(
        pageBuilder: (BuildContext context, Animation<double> animation,
            Animation secondaryAnimation) {
          return FadeTransition(
            opacity: animation,
            child: widget.nextWidget, //路由B
          );
        },
      ));
    } else {
      lastPlaying = _controller.value.isPlaying;
    }
  }
}
